package org.kd.service.impl;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.kd.dao.DeptBTODao;
import org.kd.dao.EmpBTODao;
import org.kd.model.DTO.DeptBTO;
import org.kd.model.DTO.EmpBTO;
import org.kd.service.DeptBTOService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

@Service
public class DeptBTOServiceImpl implements DeptBTOService{

	@Autowired
	EmpBTODao empBTODao;

	@Autowired
	DeptBTODao deptBTODao;
	
	@Value("${sys.webPath}")
	String webPath;
	
	@Override
	public List<DeptBTO> getFriendList(Integer id) {
		List<DeptBTO> friend = new ArrayList<DeptBTO>();
		DeptBTO deptBTO= new DeptBTO();
		// 获取所有部门
		List<DeptBTO> all = deptBTODao.selectAll();
		//迭代循环获得部门所有成员并组装
		Iterator<DeptBTO> it = all.iterator();
		while (it.hasNext()) {
			deptBTO = (DeptBTO) it.next();
			List<EmpBTO> queryByDeptId = this.empForeach(empBTODao.queryByDeptId(deptBTO.getId()));
			deptBTO.setList(queryByDeptId);
			deptBTO.setAvatar(deptBTO.getAvatar() == null || "".equals(deptBTO.getAvatar().trim())?  webPath + "/img/1.jpg" : webPath + deptBTO.getAvatar());
			friend.add(deptBTO);
		}
		return friend;
	}
	
	/**
	 *  @功能:解决头像
	 *  @作者:jh @代号:甲骨兽进化
	 *  @时间:2018年4月13日
	 *  @param queryByDeptId
	 *  @return  
	 */
	private List<EmpBTO> empForeach(List<EmpBTO> queryByDeptId) {
		// TODO Auto-generated method stub
		List<EmpBTO> list = new ArrayList<EmpBTO>();
		Iterator<EmpBTO> iterator = queryByDeptId.iterator();
		while (iterator.hasNext()) {
			EmpBTO empBTO = (EmpBTO) iterator.next();
			empBTO.setAvatar(empBTO.getAvatar() == null || "".equals(empBTO.getAvatar().trim()) ?  webPath + "/img/1.jpg" : webPath + empBTO.getAvatar());
			list.add(empBTO);
		}
		return list;
	}
	
	@Override
	public List<DeptBTO> selectAll() {
		// TODO Auto-generated method stub
		List<DeptBTO> list = new ArrayList<DeptBTO>();
		List<DeptBTO> all = deptBTODao.selectAll();
		Iterator<DeptBTO> iterator = all.iterator();
		while (iterator.hasNext()) {
			DeptBTO deptBTO = (DeptBTO) iterator.next();
			deptBTO.setAvatar(deptBTO.getAvatar() == null || "".equals(deptBTO.getAvatar().trim())?  webPath + "/img/1.jpg" : webPath + deptBTO.getAvatar());
			list.add(deptBTO);
		}
		return list;
	}

	@Override
	public List<DeptBTO> queryByEmpId(Integer id) {
		// TODO Auto-generated method stub
		List<DeptBTO> list = new ArrayList<DeptBTO>();
		List<DeptBTO> all = deptBTODao.queryByEmpId(id);
		Iterator<DeptBTO> iterator = all.iterator();
		while (iterator.hasNext()) {
			DeptBTO deptBTO = (DeptBTO) iterator.next();
			deptBTO.setAvatar(deptBTO.getAvatar() == null || "".equals(deptBTO.getAvatar().trim())?  webPath + "/img/1.jpg" : webPath + deptBTO.getAvatar());
		    list.add(deptBTO);
		}
		return list;
	}
}